package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.utils.AssertUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.business.bo.HcxMerchantPhotoAddBo;
import com.ruoyi.business.bo.HcxMerchantPhotoQueryBo;
import com.ruoyi.business.bo.HcxMerchantPhotoEditBo;
import com.ruoyi.business.domain.HcxMerchantPhoto;
import com.ruoyi.business.mapper.HcxMerchantPhotoMapper;
import com.ruoyi.business.vo.HcxMerchantPhotoVo;
import com.ruoyi.business.service.IHcxMerchantPhotoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商家门面照片Service业务层处理
 *
 * @author wsdz
 * @date 2022-08-17
 */
@Service
public class HcxMerchantPhotoServiceImpl extends ServicePlusImpl<HcxMerchantPhotoMapper, HcxMerchantPhoto> implements IHcxMerchantPhotoService {

    @Override
    public HcxMerchantPhotoVo queryById(Long id){
        return getVoById(id, HcxMerchantPhotoVo.class);
    }

    @Override
    public TableDataInfo<HcxMerchantPhotoVo> queryPageList(HcxMerchantPhotoQueryBo bo) {
        PagePlus<HcxMerchantPhoto, HcxMerchantPhotoVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), HcxMerchantPhotoVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<HcxMerchantPhotoVo> queryList(HcxMerchantPhotoQueryBo bo) {
        return listVo(buildQueryWrapper(bo), HcxMerchantPhotoVo.class);
    }

    private LambdaQueryWrapper<HcxMerchantPhoto> buildQueryWrapper(HcxMerchantPhotoQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HcxMerchantPhoto> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getPicUrl()), HcxMerchantPhoto::getPicUrl, bo.getPicUrl());
        lqw.eq(StrUtil.isNotBlank(bo.getDelFlag()), HcxMerchantPhoto::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(HcxMerchantPhotoAddBo bo) {
        HcxMerchantPhoto add = BeanUtil.toBean(bo, HcxMerchantPhoto.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(HcxMerchantPhotoEditBo bo) {
        HcxMerchantPhoto update = BeanUtil.toBean(bo, HcxMerchantPhoto.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(HcxMerchantPhoto entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importHcxMerchantPhoto(List<HcxMerchantPhotoVo> hcxMerchantPhotoList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(hcxMerchantPhotoList), "导入数据不能为空");
        int successNum = 0;
        for (HcxMerchantPhotoVo hcxMerchantPhoto : hcxMerchantPhotoList) {
            HcxMerchantPhoto add = BeanUtil.toBean(hcxMerchantPhoto, HcxMerchantPhoto.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", hcxMerchantPhotoList.size(), successNum);
        if (successNum != hcxMerchantPhotoList.size()) {
            msg = msg + String.format("失败 %d 条！", hcxMerchantPhotoList.size() - successNum);
        }
        return msg;
    }
}
